function selectSpaceGroup(space_group) {
    for (i = 0; i < document.phaserForm.elements.length; i++) {
        if (document.phaserForm.elements[i].name == "spacegroups[]" 
            && document.phaserForm.elements[i].value == space_group) {
            document.phaserForm.elements[i].checked = true;
        }
    }
}

function deselectSpaceGroup(space_group) {
    for (i = 0; i < document.phaserForm.elements.length; i++) {
        if (document.phaserForm.elements[i].name == "spacegroups[]" 
            && document.phaserForm.elements[i].value == space_group) {
            document.phaserForm.elements[i].checked = false;
        }
    }
}


function selectAllSpaceGroups(checkSetting) {
    for (i = 0; i < document.phaserForm.elements.length; i++) {
        if (document.phaserForm.elements[i].name == "spacegroups[]" 
            && document.phaserForm.elements[i].value != "ALL") {
            document.phaserForm.elements[i].checked = checkSetting;
        }
    }
}

function showTarInfo() {
    if(document.getElementById("tarInfo").style.display == "none") {
        document.getElementById("tarInfo").style.display = "block";
    } else {
        document.getElementById("tarInfo").style.display = "none";
    }
}

function populateWithExampleValues() {
    document.getElementById("sequence").value ="SEKIIHLTDDSFDTDVLKADGAILVDFWAEWCGPCKMIAPILDEIADEYQGKLTVAKLNIDQNPGTAPKYGIRGIPTLLLFKNGEVAATKVGALSKGQLKEFLDANLA";
    document.getElementById("molWeight").value = 11807;
    document.getElementById("numberInAU").value = 2;
    selectAllSpaceGroups(false);
    deselectSpaceGroup("ALL");
    selectSpaceGroup("P6");
    selectSpaceGroup("P61");
    document.getElementById("packing").value = 10;
    document.getElementById("numberOfCopiesToSearchFor").value = 1;
    addRMSD("1.0");
    
}

function calculateMW() {
    
    sequence = document.getElementById("sequence").value;
    
    if(sequence != null) {
        //convert sequence to upper case, to make it more managable.
        sequence = sequence.toUpperCase();
        sequence = sequence.replace(/[-]+/g, " ");
        sequence = sequence.replace(/[-\s\r\nUu]+/g, "");
        
        document.getElementById("sequence").value = sequence;
                            
        molecularWeight = null;
        
        for(i = 0; i < sequence.length && molecularWeight != "X"; i++) {
            aminoAcid = sequence.charAt(i);
            
            switch (aminoAcid) {
                case "A":
                  molecularWeight = molecularWeight+71.0788;
                  break;
                case "C":
                  molecularWeight = molecularWeight+103.1388;
                  break;
                case "D":
                  molecularWeight = molecularWeight+115.0886;
                  break;
                case "E":
                  molecularWeight = molecularWeight+129.1155;
                  break;
                case "F":
                  molecularWeight = molecularWeight+147.1766;
                  break;
                case "G":
                  molecularWeight = molecularWeight+57.0519;
                  break;
                case "H":
                  molecularWeight = molecularWeight+137.1411;
                  break;
                case "I":
                  molecularWeight = molecularWeight+113.1594;
                  break;
                case "K":
                  molecularWeight = molecularWeight+128.1741;
                  break;
                case "L":
                  molecularWeight = molecularWeight+113.1594;
                  break;
                case "M":
                  molecularWeight = molecularWeight+131.1926;
                  break;
                case "N":
                  molecularWeight = molecularWeight+114.1038;
                  break;
                case "P":
                  molecularWeight = molecularWeight+97.1167;
                  break;
                case "Q":
                  molecularWeight = molecularWeight+128.1307;
                  break;
                case "R":
                  molecularWeight = molecularWeight+156.1875;
                  break;
                case "S":
                  molecularWeight = molecularWeight+87.0782;
                  break;
                case "T":
                  molecularWeight = molecularWeight+101.1051;
                  break;
                case "V":
                  molecularWeight = molecularWeight+99.1326;
                  break;
                case "W" :
                  molecularWeight = molecularWeight+186.2132;
                  break;
                case "Y":
                  molecularWeight = molecularWeight+163.1760;
                  break;
                default:
                  molecularWeight = "X";
            }
        }
        
        if(molecularWeight != "X") {
            document.getElementById("molWeight").value = molecularWeight;
        } else {
            document.getElementById("molWeight").value = "";
            alert("The sequence you entered appears to be incorrect.");
        }                                                                                     
    }
}

                                                                                    
                                                                                    
                                                                                    function ValFloat(ObjectValue,AllowComma)
{
	// Used Internally by ValidateInt, ValidateFloat

	//Returns true if value is a number or is NULL
	//otherwise returns false	
	
	if (ObjectValue.length == 0) { return false };
	
	//Returns true if value is a number defined as
	//   having an optional leading + or -.
	//   having at most 1 decimal point.
	//   otherwise containing only the characters 0-9.
	var start_format = " .-0123456789";
	var number_format = " .0123456789";
	if (AllowComma == 1) number_format=number_format + ',';
	var check_char;
	var decimal = false;
	var trailing_blank = false;
	var digits = false;
	
	//The first character can be + - .  blank or a digit.
	check_char = start_format.indexOf(ObjectValue.charAt(0))
	//Was it a decimal?
	if (check_char == 1)
		{ decimal = true; }
	else if (check_char < 1)
		{ return false; }
	
	//Remaining characters can be only . or a digit, but only one decimal.
	for (var i = 1; i < ObjectValue.length; i++)
	{
		check_char = number_format.indexOf(ObjectValue.charAt(i))
		if (check_char < 0)
			return false;
		else if (check_char == 1)
		{
			if (decimal)		// Second decimal.
				return false;
			else
				decimal = true;
		}
		else if (check_char == 0)
		{
			if (decimal || digits) trailing_blank = true;
			// ignore leading blanks
		}
		else if (trailing_blank)
		{	return false; } 
		else
		{	digits = true; }
	}	
	//All tests passed, so...
	return true
}

function addRMSD(value)
{
    if (!document.getElementById("rmsdDiv_" + value)) { 
        if(ValFloat(value, false))
        {
            document.getElementById("rmsdbox").innerHTML += "<div id='rmsdDiv_" + value + "' >" + value
            + "&nbsp;<input type='hidden' name='rmsdValue[]' value='"+value+"'>"
            + "<input type='button' name='rmRMSD' value='Remove' class=\"inputButton\" onclick='removeRMSD(\"rmsdDiv_" + value + "\");'/>"
            + "<br/><br/></div>";
        }
        else
        {
            alert('RMSD value must be a floating point value');
        }
    }
    else
    {
        alert('RMSD value already exists');
    }
}

function removeRMSD(id)
{
  var rm = document.getElementById(id);
  rm.parentNode.removeChild(rm)   
}

